<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="box">
      <p>hello</p>
      <!-- 目标元素 -->
      <button data-action="save">保存</button>
      <button data-action="add">添加</button>
      <button data-action="del">删除</button>
      <button data-action="edit">编辑</button>
    </div>
    <script>
      const actions = {
        edit() {
          console.log("edit");
        },
        save() {
          console.log("save");
        },

        add() {
          console.log("add");
        },

        del() {
          console.log("del");
        },
      };

      document.getElementById("box").onclick = function (e) {
        // 获取标识标记
        const act = e.target.dataset.action;
        // action标记不存在或者标记对应方法不存，中断执行
        if (act === undefined || actions[act] === undefined) return;
        actions[act]();
      };
    </script>
  </body>
</html>
